<?php
/**
 *
 * 功能描述（蛋糕数据）
 *
 * @author suoya <jlusuoya@gmail.com>
 * @version 1
 */

class ModelCake extends ModelBase
{
	function GetList($page, $type, $type2, $vis, $cheap, $key, $keyword)
	{
		$where = "1";
		if($type != 0)
			$where .= " and type = $type";
		if($type2 != 0)
			$where .= " and type2 = $type2";
		if($vis < 2)
			$where .= " and visible = $vis";
		switch($cheap)
		{
		case 1: $where .= " and discount = 1"; break;
		case 2: $where .= " and discount < 1 and discount > 0"; break;
		case 3: $where .= " and discount = 0"; break;
		}
		if(!empty($key) && !empty($keyword))
			$where .= " and $key like '%$keyword%'";
		$data  = $this->db->getRow("goods_base", 'count(1) cnt', $where);
		if($data['cnt'] == 0)
			return array('cnt' => 0, 'pageCnt' => 0, 'data' => array());

		$retData = array('cnt' => $data['cnt'], 'pageCnt' => ceil($data['cnt'] / MSG_PAGESIZE));
		$limit = MSG_PAGESIZE * ($page - 1) . ", " . MSG_PAGESIZE;
		$fild = "`id`, `no`, `name`, `introduce`, `type`, `type2`, `visible`, `sales`, `fav_times`, `discount`, `createtime`, `updatetime`";
		$data = $this->db->getAll("goods_base", $fild, $where, 'createtime desc', $limit);
		$retData['data'] = $data;
		return $retData;
	}

	function NewCake($data1, $data2)
	{
		$id = $this->db->Insert('goods_base', $data1);
		if($id <= 0)
			return $id;
		$data2['goods_id'] = $id;
		$cnt = $this->db->Insert('goods_info', $data2);
		if($cnt == 0)
			return 0;
		return $id;
	}

	function UpdateBase($id, $data)
	{
		$cnt = $this->db->Update('goods_base', $data, "id = $id");
		return $cnt;
	}

	function UpdateInfo($id, $data)
	{
		$cnt = $this->db->Update('goods_info', $data, "goods_id = $id");
		return $cnt;
	}

	function UpdateVersion($id)
	{
		$sql = "update goods_base set version = version + 1, updatetime = CURRENT_TIMESTAMP where id = $id";
		$cnt = $this->db->execute($sql);
		return $cnt;
	}

	function UpdateVersion2($id, $price, $unit, $unprice)
	{
		$ext = "";
		if($unprice)
			$ext = ", set visible = 0";
		$sql = "update goods_base set version = version + 1, updatetime = CURRENT_TIMESTAMP, price = $price, unit = '$unit' $ext where id = $id";
		$cnt = $this->db->execute($sql);
		return $cnt;
	}

	function GetCake($id)
	{
		$fild = "`id`, `no`, `version`, `updatetime`, `createtime`, `type`, `type2`, `visible`, `name`, `introduce`, `price`, `unit`, `discount`, `fav_times`, `sales`";
		$data = $this->db->GetRow("goods_base", $fild, "id = $id");
                $data['introduce'] = explode("\n", $data['introduce']);
		return $data;
	}

	function GetBaseInfo($id)
	{
		$fild = "`id`, `no`, `name`, `discount`, `version`";
		$data = $this->db->GetRow("goods_base", $fild, "id = $id");
		return $data;
	}

	function GetInfo($id)
	{
		$fild = "`taste`, `suits`, `sweetness`, `storage`, `holiday`, `material`, `attr1`, `attr2`, `attr3`, `attr4`, `attr5`";
		$data = $this->db->GetRow("goods_info", $fild, "goods_id = $id");
		return $data;
	}

	function SetDefaultById($id, $cakeId)
	{
		$sql = "update goods_price set `default` = (id = $id) where goods_id = $cakeId";
		$this->db->execute($sql);
	}

	function GetPrices($id)
	{
		$fild = "`id`, `size`, `describe`, `price`, `unit`, `default`";
		$data = $this->db->GetAll("goods_price", $fild, "goods_id = $id", "`default` desc");
		return $data;
	}

	function AddPrice($data)
	{
		$id = $this->db->Insert("goods_price", $data);
		return $id;
	}

	function ModPrice($id, $data)
	{
		$cnt = $this->db->Update("goods_price", $data, "id = $id");
		return $cnt;
	}

	function GetComments($id)
	{
		$fild = "`version`, `user_id`, `username`, `createtime`, `content`, `taste`, `service`, `whole`";
		$data = $this->db->GetAll("goods_comment", $fild, "goods_id = $id", 'createtime desc');
		return $data;
	}

	function DelPrice($goods_id, $id)
	{
		$cnt = $this->db->Delete("goods_price", "id = $id and goods_id = $goods_id");
		return $cnt; 
	}

	function IncSalesByOrder($id)
	{
		$data = $this->db->GetAll('order_goods', 'good_id, cnt', "order_id = $id", 'cnt');
		$cnt = count($data);
		if($cnt == 0)
			return;
		$c_cnt = $data[0]['cnt'];
		$ids = array();
		for($i = 0; $i < $cnt; $i++)
		{
			if($c_cnt != $data[$i]['cnt'])	//下一组
			{
				$sql = "update goods_base set sales=sales + $c_cnt where id in (".implode(',', $ids).")";
				$this->db->execute($sql);
				$c_cnt = $data[$i]['cnt'];
				$ids = array();
			}
			$ids[] = $data[$i]['good_id'];
		}
		$sql = "update goods_base set sales=sales + $c_cnt where id in (".implode(',', $ids).")";
		$this->db->execute($sql);
	}
}

